<template>
  <view class="container">
    <!-- <view class="" :style="'padding-top:' + statusBarHeight + 'px;'"> -->
      <image
        src="../../static/image/login/start.png"
        mode="widthFix"
			  style="width: 100vw;"
      />
      		<!-- <view style="height: 860rpx; margin: auto; margin-top: 200rpx;">
            <image style="width:100%; height: 100%; background-size: 100% 100%;  " class="" src="../../static/image/login/car.png" ></image>
          </view>
          <view  style=" z-index: 2; position: absolute;bottom: 0; width:100%; height: 170rpx; padding: 0 90rpx 64rpx 90rpx;  margin: auto;" >
            <image style="width:100%; height: 100%;" class="" src="../../static/image/login/bottom.png" ></image>
          </view>
          <view> 
            <image style="position: absolute; width:100%; height: 100%; bottom: 0; background-size: 100% 100%; z-index: 1;" class="" src="../../static/image/login/image.png" ></image>
          </view> -->
    <!-- </view> -->
    <u-modal v-model="showcancel" :show-confirm-button="statusshow" :show-cancel-button="palt==='ios'?statusshow==false: statusshow==true" :confirm-text="palt==='ios'?'去AppStore':'同意'" cancel-text="拒绝" title="发现新版本" @cancel="cancel" 
      @confirm="confirm">
      <view v-if="statusshow" class="u-update-content">
        {{palt=='ios'?'请到AppStore更新':'发现新版本~'}}
      </view>
      <view v-if="!statusshow && palt=='android'" class="u-update-content">
        正在下载更新，请等待~ 
      </view>
    </u-modal>
  </view>
</template>
<script>
let that = null;
import { getVersion } from '@/api/common'
import checkver from '../../package.json'
export default {
  data() {
    return {
      statusBarHeight: 0, // 系统导航栏
      show: false,
      downloadUrl:null,
      showcancel:false,
      statusshow:true,
	  version:null,
    palt:null
    }
  },
  onReady() {

  },
  onLoad(e) {
    that = this;
    that.statusBarHeight = uni.getStorageSync("statusBarHeight");
    console.log('token', uni.getStorageSync('token'))
	  that.version=checkver.version
    let system = uni.getSystemInfoSync();
    const platform = system.platform;
	// 立即检查当前网络状态
	uni.getNetworkType({
	  success: function (res) {
		console.log('当前网络状态:', res.networkType);
		if (res.networkType === 'none') {
		  console.log('当前无网络');
		  // 你可以在这里显示提示，告知用户网络不可用
		} else {
		  console.log('网络已连接，类型:', res.networkType);
		  that.checkVersionAndToken();
		}
	  }
	});
	// 监听网络状态变化
	uni.onNetworkStatusChange(function (res) {
		console.log('网络状态变化:', res);
		// 判断是否已连接网络
		if (res.isConnected) {
		  console.log('网络已连接，类型:', res.networkType);
		  that.checkVersionAndToken()
		  
		}
	});
    // const obj = {
    //     version: checkver.version,
    //     appId: platform === 'android' ? 1 : 2
    // };
    // getVersion(obj).then(res => {
    //   this.downloadUrl = res.data.downloadUrl;
    //   this.checkshow = true;
    //   if (res.data.clientVersion !== checkver.version && res.data.updateInstall==1) {
    //     this.showcancel = true;
    //   }else{
		  // console.log('跳转');
    //       uni.switchTab({
    //       url: '/pages/delivery/index'
    //     })
      // if (uni.getStorageSync('token')) {
      //   console.log('有token')
      //   uni.switchTab({
      //     url: '/pages/delivery/index'
      //   })
      // } else {
      //   console.log('没token')
      //   // this.show = true
      //   uni.redirectTo({
      //     url:'/pages/login/login',
      //   })
      // }
    //   }
    // });
  },
  onShow() {
   
  },
  methods: {
	checkVersionAndToken(){
		let that=this
		let system = uni.getSystemInfoSync();
		const platform = system.platform;
    this.palt=platform
		const obj = {
		    version: that.version,
		    appId: platform === 'android' ? 1 : 2
		};
		 getVersion(obj).then(res => {
		   this.downloadUrl = res.data.downloadUrl;
		   this.checkshow = true;
		   if (res.data.clientVersion !== that.version && res.data.updateInstall==1) {
		     this.showcancel = true;
		   }else{
		 	  console.log('跳转');
		       uni.switchTab({
		       url: '/pages/delivery/index'
		     })
		   }
		}); 
	},
    cancel() {
      this.showcancel = false;
      plus.runtime.quit();
    },
    confirm() {
      if(this.palt=='android'){
        this.showcancel = true;
          this.show = false;  
          this.statusshow = false
        const fileExtension = this.downloadUrl.split('.').pop().toLowerCase();
        console.log('fileExtension',fileExtension);
        if (fileExtension === 'ipa') {
          this.statusshow=false
          uni.$u.toast('请到App Store进行下载');
          return;
          }else{
          const downloadTask = uni.downloadFile({
          url:this.downloadUrl,
          success: (res) => {
                console.log('下载成功',res)
                this.statusshow = true
                //安装
                plus.runtime.install(
                  res.tempFilePath, {
                    force: true
                  },
                  function(_res) {
                    plus.runtime.restart();
                  }
                )
              },
              fail: (err)=> {
                uni.$u.toast('下载失败')
              }
          })
            downloadTask.onProgressUpdate((res) => {
              console.log('下载进度' + res.progress)
              this.progress=res.progress
          })
        }
      }else{
					var url = 'https://apps.apple.com/us/app/援亲软件/id6738318034';
					// 使用系统默认浏览器打开App Store页面
					plus.runtime.openURL(url);
      }
      
    },
  },
}
</script>


<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100vh;
    overflow: hidden;
  }
  .u-update-content {
    font-size: 26rpx;
    line-height: 1.7;
    padding: 30rpx;
  }
  ::v-deep .hairline-left{
    width: 256rpx !important;
    height: 64rpx !important;
    background: #D12C25 !important;
    border-radius: 48rpx 48rpx 48rpx 48rpx !important;
    line-height: 64rpx !important;
    color: #fff !important;
    margin: 0 32rpx 0 0!important;
  }
  ::v-deep .u-mode-center-box{
    width: 687rpx;
    height: 848rpx;
    background: #FFFFFF;
    border-radius: 38rpx 38rpx 38rpx 38rpx;
  }
  ::v-deep .u-model__footer__button{
    width: 256rpx;
    height: 64rpx;
    line-height: 64rpx;
    background: rgba(0,0,0,0.2);
    border-radius: 38rpx;
  }
  ::v-deep .u-model__footer{
    display: flex;
    justify-content: space-between;
    margin: 0 0 54rpx 32rpx;
  }
  ::v-deep .u-border-top:after{
    border: none;
  }
</style>